package seminarski.gui;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSlider;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import common.point.PointG;

import seminarski.geometrija.PointSetGenerator;
import seminarski.utils.Utils;


public class Toolbar extends JPanel{
	private static final long serialVersionUID = 1L;
	private JButton animiraj;
	private JButton obrisiTacke; 
	private JRadioButton manual, random;
	private ButtonGroup type;
	private ButtonGroup pointTypes;
	private JRadioButton unif, unifHorVer, unifKrug;
	private JLabel unifL, unifHorVerL, unifKrugL;
	private PointCloudTip cloudTip = PointCloudTip.UNIF;
	private Tip tip = Tip.RANDOM;
	private JLabel spinnerLabel;
	private SpinnerNumberModel model;
	private JSpinner spinner;
	private JSlider speed;
	public Toolbar(int width, int height) {
		this.setLayout(new GridLayout(7, 2, 5, 5));
		initVariables();
		
		this.setPreferredSize(new Dimension(width, height));
		this.setVisible(true);
	}

	private void initVariables() {
		ImageIcon unifIcon = new ImageIcon("images/unif.png");
		unif = new JRadioButton("", true);
		unif.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				cloudTip = PointCloudTip.UNIF;
			}
			
		});
		unifL = new JLabel(unifIcon);
		
		ImageIcon unifHorVerIcon = new ImageIcon("images/unifHorVer.png");
		unifHorVer = new JRadioButton("", false);
		unifHorVer.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				cloudTip = PointCloudTip.UNIF_HORVER;
			}
			
		});
		unifHorVerL = new JLabel(unifHorVerIcon);
		
		ImageIcon unifKrugIcon = new ImageIcon("images/unifKrug.png");
		unifKrug = new JRadioButton("", false);
		unifKrug.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				cloudTip = PointCloudTip.UNIF_KRUG;
			}
			
		});
		unifKrugL = new JLabel(unifKrugIcon);
		
		pointTypes = new ButtonGroup();
		pointTypes.add(unif);
		pointTypes.add(unifHorVer);
		pointTypes.add(unifKrug);
		
		spinnerLabel = new JLabel("Broj tacaka");
		model = new SpinnerNumberModel(20, 3, 500, 1);
		spinner = new JSpinner(model);
		spinner.addChangeListener(new ChangeListener(){

			@Override
			public void stateChanged(ChangeEvent e) {
				int newValue = Integer.parseInt(spinner.getModel().getValue().toString());
				MyCanvas canvas = App.getApp().getCanvas();
				canvas.setPointCount(newValue);
				
			}
			
		});
		speed = new JSlider(1000, 5000, 1000);
		speed.addChangeListener(new ChangeListener(){

			@Override
			public void stateChanged(ChangeEvent e) {
				MyCanvas canvas = App.getApp().getCanvas();
				int newValue = speed.getValue();
				canvas.setSpeed(newValue);
				
			}
			
		});
		type = new ButtonGroup();

		random = new JRadioButton("Random", true);
		random.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				tip = Tip.RANDOM;
				MyCanvas canvas = App.getApp().getCanvas();
				canvas.getPoints().clear();
				canvas.getConvexHull().getHull().clear();
				canvas.repaint();
				
			}
			
		});
		manual = new JRadioButton("Manual", false);
		manual.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				tip = Tip.MANUAL;
				MyCanvas canvas = App.getApp().getCanvas();
				canvas.getPoints().clear();
				canvas.getConvexHull().getHull().clear();
				canvas.clearSegments();
				canvas.repaint();
			}
			
		});


		
		type.add(random);
		type.add(manual);

		obrisiTacke = new JButton("Obrisi sve");
		obrisiTacke.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				MyCanvas canvas = App.getApp().getCanvas();
				canvas.getPoints().clear();
				canvas.repaint();
			}
			
		});
		animiraj = new JButton("Animiraj");
		animiraj.setMnemonic(KeyEvent.VK_A);
		animiraj.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				final MyCanvas canvas = App.getApp().getCanvas();
				switch (tip) {
				case RANDOM:
					if(cloudTip == PointCloudTip.UNIF)
					{						
						canvas.setPoints(PointSetGenerator.uniformno(100, 450, 100, 450, canvas.getPointCount()));	
					}
					else if(cloudTip == PointCloudTip.UNIF_HORVER){
						canvas.setPoints(PointSetGenerator.izduzeno(Utils.nextDouble(40, 200), 
								Utils.nextDouble(201, 490), Utils.nextDouble(40, 200), Utils.nextDouble(201, 490), 
								canvas.getPointCount()));
					}
					else{
						canvas.setPoints(PointSetGenerator.poDvaKruga(
								new PointG<Double>(Utils.nextDouble(100, 150), Utils.nextDouble(150, 200)), 
								Utils.nextDouble(50, 100), 
								new PointG<Double>(Utils.nextDouble(350, 500), Utils.nextDouble(350, 500)), 
								Utils.nextDouble(40, 70), canvas.getPointCount()));
					}
					break;

				case MANUAL:
					break;
				}
				
				Thread th = new Thread(new Runnable(){

					@Override
					public void run() {
						
						canvas.getConvexHull().convexHull(canvas.getPoints());//);
						canvas.crtajPoligon(canvas.getConvexHull().hullSize() - 1);
						canvas.setIscrtavasePoligon(false);
						canvas.iscrtajTocak();
					}
					
				});
				th.start();
				canvas.repaint();
				canvas.iscrtavaSeTocak = false;
				
				canvas.resetTocak();				
			}
			
		});
		
		addAll();
		
	}
	
	
	private void addAll() {
		this.add(unif);
		this.add(unifL);
		this.add(unifHorVer);
		this.add(unifHorVerL);
		this.add(unifKrug);
		this.add(unifKrugL);
		this.add(new JLabel("Brzina"));
		this.add(speed);
		this.add(spinnerLabel);
		this.add(spinner);
		this.add(random);
		this.add(manual);
		this.add(new JLabel());
//		this.add(obrisiTacke);
		this.add(animiraj);
	}


	public enum PointCloudTip{
		UNIF, UNIF_KRUG, UNIF_HORVER;
	}
	public enum Tip{
		RANDOM, MANUAL;
	}
	
	public Tip getTip() {
		return tip;
	}

	public void setTip(Tip tip) {
		this.tip = tip;
	}

	public JRadioButton getManual() {
		return manual;
	}

	public void setManual(boolean b) {
		this.manual.setSelected(b);
		this.random.setSelected(!b);
	}

	public JRadioButton getRandom() {
		return random;
	}

	public void setRandom(boolean b) {
		this.manual.setSelected(!b);
		this.random.setSelected(b);
	}
	
	
}
